/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.avbravo.m21couchdb;

import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.StatusDisplayer;
import org.openide.util.ImageUtilities;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.avbravo.m21couchdb//M21Couchdb//EN",
autostore = false)
public final class M21CouchdbTopComponent extends TopComponent {

    private static M21CouchdbTopComponent instance;
    /** path to the icon used by the component and its open action */
    static final String ICON_PATH = "org/avbravo/m21couchdb/marc.jpg";
    private static final String PREFERRED_ID = "M21CouchdbTopComponent";
    Sesion sesion;
    int msgTypeError = NotifyDescriptor.ERROR_MESSAGE;
    int msgType = NotifyDescriptor.INFORMATION_MESSAGE;
    NotifyDescriptor d;
    String prefijo;
boolean RegistrosBasesDatos = false;
    public M21CouchdbTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(M21CouchdbTopComponent.class, "CTL_M21CouchdbTopComponent"));
        setToolTipText(NbBundle.getMessage(M21CouchdbTopComponent.class, "HINT_M21CouchdbTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        sesion = new Sesion();
       
        jComboBoxDataBases.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelMigrar = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldIpCouchdb = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldPuertoCouchdb = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldUserCouchdb = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jPasswordFieldCouchdb = new javax.swing.JPasswordField();
        jButtonProbarConexion = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButtonMigrar = new javax.swing.JButton();
        jRadioButtonActualizar = new javax.swing.JRadioButton();
        jComboBoxDataBases = new javax.swing.JComboBox();
        jRadioButtonNuevo = new javax.swing.JRadioButton();
        jTextFieldPrefijoInicial = new javax.swing.JTextField();
        jTextFieldPrefijo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldArchivo = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldRuta = new javax.swing.JTextField();
        jPanelHistorial = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMensajes = new javax.swing.JTextArea();
        jButtonVerMensajes = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(986, 484));

        jPanelMigrar.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPanelMigrar.border.title"))); // NOI18N
        jPanelMigrar.setPreferredSize(new java.awt.Dimension(474, 384));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPanel5.border.title"))); // NOI18N
        jPanel5.setPreferredSize(new java.awt.Dimension(482, 259));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/m21couchdb/couchdb-icon-16px.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel4.text")); // NOI18N

        jTextFieldIpCouchdb.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldIpCouchdb.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel5.text")); // NOI18N

        jTextFieldPuertoCouchdb.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldPuertoCouchdb.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel6.text")); // NOI18N

        jTextFieldUserCouchdb.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldUserCouchdb.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel7.text")); // NOI18N

        jPasswordFieldCouchdb.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPasswordFieldCouchdb.text")); // NOI18N

        jButtonProbarConexion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/m21couchdb/couchdb-icon-16px.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jButtonProbarConexion, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jButtonProbarConexion.text")); // NOI18N
        jButtonProbarConexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProbarConexionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel6)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldIpCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldPuertoCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldUserCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPasswordFieldCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonProbarConexion)))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldIpCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldPuertoCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldUserCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jPasswordFieldCouchdb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonProbarConexion))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        org.openide.awt.Mnemonics.setLocalizedText(jButtonMigrar, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jButtonMigrar.text")); // NOI18N
        jButtonMigrar.setEnabled(false);
        jButtonMigrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMigrarActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButtonActualizar);
        org.openide.awt.Mnemonics.setLocalizedText(jRadioButtonActualizar, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jRadioButtonActualizar.text")); // NOI18N
        jRadioButtonActualizar.setEnabled(false);
        jRadioButtonActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonActualizarActionPerformed(evt);
            }
        });

        jComboBoxDataBases.setEnabled(false);

        buttonGroup1.add(jRadioButtonNuevo);
        jRadioButtonNuevo.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(jRadioButtonNuevo, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jRadioButtonNuevo.text")); // NOI18N
        jRadioButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNuevoActionPerformed(evt);
            }
        });

        jTextFieldPrefijoInicial.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldPrefijoInicial.text")); // NOI18N
        jTextFieldPrefijoInicial.setEnabled(false);
        jTextFieldPrefijoInicial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPrefijoInicialActionPerformed(evt);
            }
        });

        jTextFieldPrefijo.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldPrefijo.text")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButtonActualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxDataBases, 0, 273, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButtonNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldPrefijoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldPrefijo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addComponent(jButtonMigrar, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonNuevo)
                    .addComponent(jTextFieldPrefijoInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldPrefijo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxDataBases, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jButtonMigrar)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPanel3.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel1.text")); // NOI18N

        jTextFieldArchivo.setEditable(false);
        jTextFieldArchivo.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldArchivo.text")); // NOI18N

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/m21couchdb/top-search-icon.gif"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jButtonSearch, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jButtonSearch.text")); // NOI18N
        jButtonSearch.setToolTipText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jButtonSearch.toolTipText")); // NOI18N
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jLabel2.text")); // NOI18N

        jTextFieldRuta.setText(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jTextFieldRuta.text")); // NOI18N
        jTextFieldRuta.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jTextFieldRuta, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jTextFieldArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(82, 82, 82))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextFieldArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldRuta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelMigrarLayout = new javax.swing.GroupLayout(jPanelMigrar);
        jPanelMigrar.setLayout(jPanelMigrarLayout);
        jPanelMigrarLayout.setHorizontalGroup(
            jPanelMigrarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMigrarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelMigrarLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(115, 115, 115))
            .addGroup(jPanelMigrarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        jPanelMigrarLayout.setVerticalGroup(
            jPanelMigrarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMigrarLayout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPanelMigrar.TabConstraints.tabTitle"), jPanelMigrar); // NOI18N

        jPanelHistorial.setPreferredSize(new java.awt.Dimension(608, 474));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextAreaMensajes.setColumns(20);
        jTextAreaMensajes.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMensajes);

        jButtonVerMensajes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/avbravo/m21couchdb/noNewMessages.gif"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jButtonVerMensajes, org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jButtonVerMensajes.text")); // NOI18N
        jButtonVerMensajes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerMensajesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelHistorialLayout = new javax.swing.GroupLayout(jPanelHistorial);
        jPanelHistorial.setLayout(jPanelHistorialLayout);
        jPanelHistorialLayout.setHorizontalGroup(
            jPanelHistorialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelHistorialLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelHistorialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonVerMensajes)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(244, Short.MAX_VALUE))
        );
        jPanelHistorialLayout.setVerticalGroup(
            jPanelHistorialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelHistorialLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jButtonVerMensajes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(309, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(M21CouchdbTopComponent.class, "M21CouchdbTopComponent.jPanelHistorial.TabConstraints.tabTitle"), jPanelHistorial); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 599, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        try {
            JFileChooser chooser = new JFileChooser("");
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = chooser.showOpenDialog((java.awt.Component) null);

            if (returnVal == chooser.APPROVE_OPTION) {

                java.io.File inFile = chooser.getSelectedFile();
                jTextFieldArchivo.setText(inFile.getName());
                jTextFieldRuta.setText(inFile.getCanonicalPath().toString());
                jButtonMigrar.setEnabled(true);
            }
        } catch (Exception ex) {

            d = new NotifyDescriptor.Message(ex.getLocalizedMessage().toString(), msgTypeError);
            DialogDisplayer.getDefault().notify(d);

        }

        //  processFile(inFile);


    }//GEN-LAST:event_jButtonSearchActionPerformed

private void jButtonMigrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMigrarActionPerformed
    // TODO add your handling code here:
    try {
        String urlArchivo = jTextFieldRuta.getText();
        if (urlArchivo == null || urlArchivo.equals("")) {
            d = new NotifyDescriptor.Message("Debe seleccionar un archivo marc21", msgType);
            DialogDisplayer.getDefault().notify(d);
            return;
        }
        sesion.setNombreArchivo(jTextFieldArchivo.getText());
        sesion.ListaMensajes.clear();
        sesion.setUrlArchivo(urlArchivo);
        jTextAreaMensajes.setText("");
        if (ObtenerPrefijo() == false) {
            return;
        }

        if (jRadioButtonNuevo.isSelected()) {
            sesion.setNuevo("si");
            sesion.setProceso("nuevo archivo");

            if (sesion.CrearBaseDatos(prefijo) == false) {
                d = new NotifyDescriptor.Message("Ya existe esa base de datos. \n Esto indica que no es un archivo nuevo", msgType);
                DialogDisplayer.getDefault().notify(d);
                return;
            }
            if (sesion.AbrirBaseDatos(prefijo) == false) {
                d = new NotifyDescriptor.Message("No se puede abrir la base de datos", msgType);
                DialogDisplayer.getDefault().notify(d);
                return;
            }

        } else {
            if (jRadioButtonActualizar.isSelected()) {
                sesion.setNuevo("no");
                sesion.setProceso("archivo actualizado");


                if (sesion.AbrirBaseDatos(prefijo) == false) {
                    d = new NotifyDescriptor.Message("No existe esa base de datos. " + prefijo + "\n esto indica que es un archivo nuevo.", msgType);
                    DialogDisplayer.getDefault().notify(d);
                    return;
                }
            } else {
                d = new NotifyDescriptor.Message("Seleccione si es un archivo nuevo o para actualizar", msgType);
                DialogDisplayer.getDefault().notify(d);
                return;
            }

        }
        jButtonVerMensajes.setEnabled(true);
jButtonMigrar.setEnabled(false);
        StatusDisplayer.getDefault().setStatusText("Iniciando proceso de migración");
        Thread t = new Thread(new NBRunnable());
        t.start();

    } catch (Exception ex) {

        d = new NotifyDescriptor.Message(ex.getLocalizedMessage().toString(), msgTypeError);
        DialogDisplayer.getDefault().notify(d);

    }



}//GEN-LAST:event_jButtonMigrarActionPerformed

    private boolean ObtenerPrefijo() {
        try {
            if (jRadioButtonNuevo.isSelected()) {
                prefijo = jTextFieldPrefijo.getText().trim();
                if (prefijo == null || prefijo.equals("")) {
                    d = new NotifyDescriptor.Message("Indique el prefijo para la base datos Couchdb", msgType);
                    DialogDisplayer.getDefault().notify(d);
                    jTextFieldPrefijo.requestFocus();
                    return false;
                }
                prefijo = jTextFieldPrefijoInicial.getText().trim() + prefijo.trim();
                sesion.setPrefijo(prefijo);
                return true;
            } else {
                if (jRadioButtonActualizar.isSelected()) {

                    prefijo = jComboBoxDataBases.getSelectedItem().toString();
                    if (prefijo == null || prefijo.equals("")) {
                        d = new NotifyDescriptor.Message("Seleccione la base datos Couchdb", msgType);
                        DialogDisplayer.getDefault().notify(d);
                        jComboBoxDataBases.requestFocus();
                        return false;
                    }

                    sesion.setPrefijo(prefijo);

                    return true;
                }
            }
        } catch (Exception ex) {

            d = new NotifyDescriptor.Message("ObtenerPrefijo() " + ex.getLocalizedMessage().toString(), msgTypeError);
            DialogDisplayer.getDefault().notify(d);

        }
        return false;
    }
private void jTextFieldPrefijoInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPrefijoInicialActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldPrefijoInicialActionPerformed

private void jButtonProbarConexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProbarConexionActionPerformed
    // TODO add your handling code here:
    try {
        String ip = jTextFieldIpCouchdb.getText();
        if (ip == null || ip.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el ip", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIpCouchdb.requestFocus();
            return;
        }
        String spuerto = jTextFieldPuertoCouchdb.getText();
        if (spuerto == null || spuerto.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el puerto", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldPuertoCouchdb.requestFocus();
            return;
        }
        int puerto = Integer.parseInt(spuerto);

        String user = jTextFieldUserCouchdb.getText();
        if (user == null) {
            user = "";

        }
        String password = new String(this.jPasswordFieldCouchdb.getPassword());
        if (password == null) {
            password = "";

        }
        sesion.setIpCouchdb(ip);
        sesion.setPuertoCouchdb(puerto);
        sesion.setUserCouchdb(user);
        sesion.setPasswordCouchdb(password);
        boolean couchdb = sesion.ConectarCouchDB();
        if (couchdb == false) {
            d = new NotifyDescriptor.Message("No se pudo establecer la Conexión a Couchdb", msgType);
            DialogDisplayer.getDefault().notify(d);
            return;
        }
        jComboBoxDataBases.removeAllItems();
        for (int i = 0; i < sesion.databasesCouchdb.size(); i++) {
            String name = sesion.databasesCouchdb.get(i).toString();
            name = name.trim();
            if (name == null || name.equals("")) {
            } else {
                if (name.length() > 9) {
                    String tmp = name.substring(0, 10);
                    if (tmp.equals("marc_json_")) {
                        jComboBoxDataBases.addItem(sesion.databasesCouchdb.get(i).toString());
                    }

                }
                //marc_json_

            }

        }
        this.RegistrosBasesDatos = false;
        if (jComboBoxDataBases.getItemCount() != 0) {
            this.RegistrosBasesDatos = true;
          
        }
        jRadioButtonActualizar.setEnabled( this.RegistrosBasesDatos);
        StatusDisplayer.getDefault().setStatusText("Conexión exitosa a couchdb");

       
        d = new NotifyDescriptor.Message("Conexión exitosa a Couchdb", msgType);
        DialogDisplayer.getDefault().notify(d);
    } catch (Exception ex) {
        d = new NotifyDescriptor.Message(ex.getLocalizedMessage().toString(), msgTypeError);
        DialogDisplayer.getDefault().notify(d);
    }
}//GEN-LAST:event_jButtonProbarConexionActionPerformed

private void jRadioButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNuevoActionPerformed
    // TODO add your handling code here:
    jComboBoxDataBases.setEnabled(false);
    jTextFieldPrefijo.setEnabled(true);
}//GEN-LAST:event_jRadioButtonNuevoActionPerformed

private void jButtonVerMensajesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerMensajesActionPerformed
    // TODO add your handling code here:
    for (int i = 0; i < sesion.getListaMensajes().size(); i++) {
        jTextAreaMensajes.append(sesion.getListaMensajes().get(i) + "\n");
    }
}//GEN-LAST:event_jButtonVerMensajesActionPerformed

private void jRadioButtonActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonActualizarActionPerformed
    // TODO add your handling code here:
    jComboBoxDataBases.setEnabled(true);
    jTextFieldPrefijo.setEnabled(false);
}//GEN-LAST:event_jRadioButtonActualizarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButtonMigrar;
    private javax.swing.JButton jButtonProbarConexion;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JButton jButtonVerMensajes;
    private javax.swing.JComboBox jComboBoxDataBases;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanelHistorial;
    private javax.swing.JPanel jPanelMigrar;
    private javax.swing.JPasswordField jPasswordFieldCouchdb;
    private javax.swing.JRadioButton jRadioButtonActualizar;
    private javax.swing.JRadioButton jRadioButtonNuevo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextAreaMensajes;
    private javax.swing.JTextField jTextFieldArchivo;
    private javax.swing.JTextField jTextFieldIpCouchdb;
    private javax.swing.JTextField jTextFieldPrefijo;
    private javax.swing.JTextField jTextFieldPrefijoInicial;
    private javax.swing.JTextField jTextFieldPuertoCouchdb;
    private javax.swing.JTextField jTextFieldRuta;
    private javax.swing.JTextField jTextFieldUserCouchdb;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized M21CouchdbTopComponent getDefault() {
        if (instance == null) {
            instance = new M21CouchdbTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the M21CouchdbTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized M21CouchdbTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(M21CouchdbTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof M21CouchdbTopComponent) {
            return (M21CouchdbTopComponent) win;
        }
        Logger.getLogger(M21CouchdbTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_NEVER;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }
}
